package com.everflourish.yeah100.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

import java.util.List;

public class MyHScrollView extends HorizontalScrollView {

    private List<MyHScrollView> myHScrollViews;

    public MyHScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyHScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyHScrollView(Context context) {
        super(context);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        // 当前的CHSCrollView被触摸时，滑动其它
        if (myHScrollViews != null) {
            for (MyHScrollView scrollView : myHScrollViews) {
                // 防止重复滑动
                if (this != scrollView)
                    scrollView.smoothScrollTo(l, t);
            }
        } else {
            super.onScrollChanged(l, t, oldl, oldt);
        }
    }

    public void moveToLeft() {
        for (int i = 0; i < getWidth(); i++) {
            if (myHScrollViews != null) {
                for (MyHScrollView scrollView : myHScrollViews) {
                    // 防止重复滑动
                    if (this != scrollView)
                        scrollView.smoothScrollTo(0, 0);
                }
            }
        }
    }

    public void moveToRight() {
        if (myHScrollViews != null) {
            for (MyHScrollView scrollView : myHScrollViews) {
                //为什么要移动两次？搞不懂
                scrollView.smoothScrollTo(getWidth(), 0);
                scrollView.smoothScrollTo(getWidth(), 0);
            }
        }
    }

    public List<MyHScrollView> getMyHScrollViews() {
        return myHScrollViews;
    }

    public void setMyHScrollViews(List<MyHScrollView> myHScrollViews) {
        this.myHScrollViews = myHScrollViews;
    }
}